************************************************ 
* Estiamte country-specific information friction
************************************************


local ratingcutoff = 25
set more off

* gen first week price
use "data.dta", clear
gen week = week(dofc(time))
gen year = year(dofc(time))
replace week = (year-2014)*52+week
bys productid week: egen pwquan = sum(quantityinpiece)
bys productid week: egen pwrev = sum(transactionvalue)
gen price1 = pwrev/pwquan
bys productid: egen minweek = min(week)
duplicates drop productid week, force
keep if week==minweek
keep productid price1
tempfile firstprice
save `firstprice'

* gen weekly price
use "data.dta", clear
gen week = week(dofc(time))
gen year = year(dofc(time))
replace week = (year-2014)*52+week
bys productid week: egen pwquan = sum(quantityinpiece)
bys productid week: egen pwrev = sum(transactionvalue)
gen pwprice = pwrev/pwquan
duplicates drop productid week, force
keep productid week pwprice
tsset productid week
tsfill
replace pwprice = l.pwprice if pwprice==.
tempfile weekprice
save `weekprice'

* keep first week's order by country
use "data.dta", clear
keep if prevordernum ==0
gen week = week(dofc(time))
gen year = year(dofc(time))
replace week = (year-2014)*52+week
bys productid buyercountry week: egen pwcquan1 = sum(quantityinpiece)
bys productid buyercountry week: gen pwcorder1 = _N
duplicates drop productid buyercountry week, force
bys productid: egen minweek = min(week)
keep if week == minweek
keep productid buyercountry pwcquan1 pwcorder1
tempfile pwcfirst
save `pwcfirst'

* select product-weeks with more than 25 ratings
use "data.dta", clear
keep if prevordernum == 0
gen week = week(dofc(time))
gen year = year(dofc(time))
replace week = (year-2014)*52+week
bys productid week: egen ratingnum = sum(buyerrating!=.)
bys productid week: gen ordernum = _N
keep productid week ratingnum ordernum
duplicates drop productid week, force
tsset productid week
tsfill
replace ratingnum = 0 if ratingnum==.
replace ordernum = 0 if ordernum==.
gen w24ratingnum = 0
gen w24ordernum = 0
foreach i of numlist 1/24{
	gen l`i'_ratingnum = l`i'.ratingnum
	replace l`i'_ratingnum = 0 if l`i'_ratingnum==.
	replace w24ratingnum = w24ratingnum + l`i'_ratingnum
	
	gen l`i'_ordernum = l`i'.ordernum
	replace l`i'_ordernum = 0 if l`i'_ordernum==.
	replace w24ordernum = w24ordernum + l`i'_ordernum

}
keep if w24ratingnum>=`ratingcutoff'
//keep if w24ordernum>=`ratingcutoff'
keep productid week
tempfile pwr
save `pwr'

* construct product-country-week
use "data.dta", clear
keep productid buyercountry
duplicates drop productid buyercountry, force
fillin productid buyercountry
drop  _fillin
joinby productid using `pwr'
keep if week!=.
tempfile pcwfinal
save `pcwfinal'

use "data.dta", clear
gen week = week(dofc(time))
gen year = year(dofc(time))
replace week = (year-2014)*52+week
*
gen decoration = 1 if itemdecoration!=""
replace decoration = 0 if strpos(itemdecoration,"None")
replace decoration=0 if itemdecoration==""
gen lnpic = ln(picnumtotal+1)
egen medlnpic = mean(lnpic)
gen picmore = lnpic>medlnpic
*
bys productid buyercountry week: egen pwcquan = sum(quantityinpiece)
bys productid buyercountry week: gen pwcorder = _N
keep productid buyercountry week pwcquan pwcorder decoration picmore
duplicates drop productid buyercountry week, force
merge 1:1 productid buyercountry week using `pcwfinal'
drop if _merge==1
drop _merge
merge n:1 productid buyercountry using `pwcfirst'
drop if _merge==2 // not all pwc can get 25 ratings in the end
drop _merge
merge n:1 productid using `firstprice'
drop if _merge==2
drop _merge
merge n:1 productid week using `weekprice'
drop if _merge==2
drop _merge
replace pwcquan1 = 0 if pwcquan1==.
replace pwcorder1 = 0 if pwcorder1==.
replace pwcquan = 0 if pwcquan==.
replace pwcorder = 0 if pwcorder==.
gen pwcquandifln = ln(pwcquan+1)- ln(pwcquan1+1)
gen pwcorderdifln = ln(pwcorder+1) - ln(pwcorder1+1)
gen pwcquandif0ln = ln(pwcquan) - ln(pwcquan1)
gen lnpwprice = ln(pwprice)
gen lnprice1 = ln(price1)
gen pwcquandif = pwcquan-pwcquan1

* merge with gravity variable
merge m:1 buyercountry using "distcodegdp.dta"
drop if _merge==2
drop _merge
merge m:1 numcountrycode using "worldpopulation.dta"
drop if _merge==2
drop _merge
merge m:1 numcountrycode using "chnmigrants.dta"
drop if _merge==2
drop _merge
gen chnmshare = chnmigrants/(pop2015thousand*1000)
gen chinargdppc = 6069.7207
gen rgdppcdif = gdpperc2012usd - chinargdppc
gen lnrgdppc = ln(gdpperc2012usd)
gen absrgdppcdif = abs(rgdppcdif)
gen lnabsrgdppcdif = ln(absrgdppcdif)
gen rgdpdif2 = rgdppcdif^2
gen lndistw = ln(distw)

gen hlndistw = lndistw * decoration
gen hcontig = contig * decoration
gen hlnabsrgdppcdif = lnabsrgdppcdif * decoration
gen hchnmshare = chnmshare * decoration

/*
gen hlndistw = lndistw * picmore
gen hcontig = contig * picmore
gen hlnabsrgdppcdif = lnabsrgdppcdif * picmore
gen hchnmshare = chnmshare * picmore
*/
egen pwgroup = group(productid week)
xtset pwgroup
eststo quan_all1: xtreg pwcquandif lndistw contig lnabsrgdppcdif  chnmshare if buyercountry !="Hong Kong" & buyercountry !="Macau" , fe i(pwgroup) cl(pwgroup)
eststo quan_all1: xtreg pwcquandif lndistw contig lnabsrgdppcdif  chnmshare hlndistw hcontig hlnabsrgdppcdif  hchnmshare if buyercountry !="Hong Kong" & buyercountry !="Macau" , fe i(pwgroup) cl(pwgroup)


esttab *_all*,  star(* 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par))  stats(r2 N) mti

